//
//  OAuthViewController.swift
//  Stauts
//
//  Created by Kylin on 16/3/14.
//  Copyright © 2016年 Kylin. All rights reserved.
//

import UIKit
import SVProgressHUD

class OAuthViewController: UIViewController {

    // MARK:-  控件属性
    @IBOutlet weak var webView: UIWebView!
    
    // MARK:- 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 加载导航栏
        setupNavgationBar()
        
        // 加载登陆介面
        loadPage()
    }


}

extension OAuthViewController {
    
    private func setupNavgationBar () {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关团", style: .Plain, target: self, action: #selector(OAuthViewController.close))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "填充", style: .Plain, target: self, action: #selector(OAuthViewController.autoFill))
        
        title = "登陆页面"
    }
    private func loadPage () {
        // 1:获取url
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(app_key)&redirect_uri=\(redirect_uri)"
        // 2:创建url
        guard let url = NSURL(string: urlString) else {
            return
        }
        // 3:添加url请求对象
        webView.loadRequest(NSURLRequest(URL: url))
        webView.scalesPageToFit = true
        
    }
    
    // MARK:- 监听导航按钮的点击
    @objc private func close () {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc private func autoFill () {
        // 1.获取要执行的js(JavaScript)代码
        let jsCode = "document.getElementById('userId').value='1606020376@qq.com';document.getElementById('passwd').value='haomage';"
        // webView加载js代码
        webView.stringByEvaluatingJavaScriptFromString(jsCode)
    }
}

// MARK:- UIWebViewDelegate
extension OAuthViewController : UIWebViewDelegate {
    
    func webViewDidStartLoad(webView: UIWebView) {
        
        SVProgressHUD.show()
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        
        SVProgressHUD.dismiss()
    }
    
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        SVProgressHUD.dismiss()
    }
    
    // 加载某一个页面时会执行该方法
    // 返回值 : true : 继续加载该页面 false : 不会加载该页面
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        //1:获取对应的字符串url
        guard let urlString = request.URL?.absoluteString else {
            return true
        }
        // 2.判断字符串中是否有code
        if !urlString.containsString("code=") {
            return true
        }
        
        // 3:取出codeString
        guard let codeString = urlString.componentsSeparatedByString("code=").last else {
            return true
        }
        
        // 4.换取AccessToken
        UserAccountViewModel.shareManager.loadAccessToken(codeString) { (isSuccess) -> () in
            if !isSuccess {
                return
            }
            
            self.dismissViewControllerAnimated(false, completion: { () -> Void in
                
                // 显示欢迎介面
                UIApplication.sharedApplication().keyWindow?.rootViewController = WelcomViewController()
            })
        }
        return false
    }
}









